Repository navigation
fix(libkernel): make cond relock and pthread_once waits alertable on Windows - #27985
kiansaeidnia wants to merge 1 commit into
Conversation
…Windows On Windows, a woken scePthreadCondWait or scePthreadCondTimedwait that loses the spin now takes the mutex back through TimedWait::ParkAcquire, as scePthreadMutexLock does, instead of the native lock. pthread_once waiters wait on a TimedWait::Condition instead of a std::condition_variable, on every host. Both waits are alertable on Windows, so a raise at a thread blocked behind a stopped thread is delivered there and the wait resumes, as FreeBSD 11.4 restarts umtx mutex and once waits after a signal handler. On Linux and macOS the relock still uses the native lock and pthread_once behaves as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
Plyxen
left a comment
There was a problem hiding this comment.
Checked head 555fff5dec. All on Windows 11 with WinLibs GCC 15.2 (Release), applied to main 4c7578e0a4 together with #28006, #28002, #28001, #27991, #27987, #28007, #27994. tools/check_conventions.py --head and tools/debt_fold.py --check passed and the full build succeeded. In the full CPU suite (5,135 tests) the only failure is windows_read_trap_unknown, which fails on main too (fixed in #28028).
guest_raise_leaves_host_code passes. Windows is the platform the alertable ParkAcquire and pthread_once waits are for.
AI-assisted: yes (Claude Code)
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
Nothing blocks this change; the points below are optional.
What I ran
Reviewed head 555fff5dec658050755d95afaac75bcbc7d54d03. No commands or tests were run; source was inspected through the pinned reader.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed source | passed | Four changed files read |
| Tests | not run | Author-reported results were not independently verified |
Findings
Blocking
None.
Non-blocking
- In
GuestRaiseLeavesHostCode.cpp, the relock setup loops onrelock.waiting, which is set before the waiter enters the condition wait (:145); the fixed sleeps then assume the signal and mutex reacquisition have progressed to the intended blocking point (:172-177). Consider adding explicit synchronization or an observable wait-state check so this test cannot pass or fail based on scheduling.
Not verified
- No test execution, build, title run, or physical-console behavior was verified. Test reports in the description are author-reported.
- All changed files listed in the manifest were read:
Once.cpp,Cond.cpp,GuestRaiseLeavesHostCode.cpp, and the technical debt change note. No changed files were excluded in the manifest.files.jsonwas not available as a review-folder document.
To get this to a good state
Nothing is required. The optional test synchronization improvement is noted above.
ligmaSec
left a comment
There was a problem hiding this comment.
The changed pthread_once implementation, condition wait implementation, and raise fixture passed MinGW syntax and type checking. I traced the blocking relock to TimedWait::ParkAcquire and the once wait to TimedWait::Condition::Wait. The local MinGW backend could not emit all three objects, so the five Windows runtime cases remain unverified here.
AI-assisted: yes.
|
Windows tester result at head
|
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files and the functions each diff names before concluding that there is no overlap.
|
What
On Windows,
sceKernelRaiseExceptionreaches a target in host code as an APC only while it is in a libkernel wait. Two guest-visible waits did not go through one, so a raise at a thread blocked in them was never delivered:scePthreadCondWait,scePthreadCondTimedwait,pthread_cond_waitandpthread_cond_timedwait, and for libc's libc++std::condition_variable, which forwards to them. After a wake,WakingLock::lockcalled the native mutex lock once spinning failed.pthread_oncewaiters. They blocked on astd::condition_variable.This is the shape of the Unity/IL2CPP stop-the-world freeze:
#1562 fixed this for
scePthreadMutexLockand rwlock waits after a measured God of War: Sons of Sparta (PPSA28997) freeze, and #10666 (Graveyard Keeper 2, PPSA35943) recorded the general gap.The change:
TimedWait::ParkAcquire, keyed and woken the wayscePthreadMutexLockis.WakingLock::unlockandscePthreadMutexUnlockalready unpark that key.pthread_oncewaits on aTimedWait::Condition.PCATCHsleeps, then a retry in libthr).TimedWait::Conditionis acondition_variable_any, sopthread_oncebehaves as before.The debt entry keeps its general wording about waits on an already stopped thread, and lists the waits that remain as examples:
pthread_barrier_wait,scePthreadOnce, libc_Mtx_lock,std::call_onceand__cxa_guard_acquire. Two of these are left out on purpose:scePthreadOnceis rewritten by #25283, and Dinkumware_Mtx_lock/_Cnd_waitby #18347.pr_overlapcould not compareOnce.cppwith #18190, because that PR conflicts withmain. Its change to the file is a rename ofpthread_once_nid_postfixto_nid_no_patchplus a forwarder, so whichever merges second needs a textual rebase. #26174 rewriteswaitUntilinCond.cppbut leavesWakingLock::lockalone.Tested
Windows 11, MinGW-w64 GCC 16.2, Release.
guest_raise_leaves_host_codegains five cases, each run for 10 rounds:scePthreadCondWait, with a normal mutex and with a recursive mutex held 3 times (the count is restored);scePthreadCondTimedwait;pthread_oncewhose initializer thread is stopped in its raise handler.In the relock cases the main thread holds the mutex while the woken waiter blocks, which is the same delivery point as a stopped holder.
In each case the handler must acknowledge within 2 s. Afterwards the wait must return 0 with the mutex owned, and the initializer must have run exactly once. Each relock round raises twice: the second raise is sent only after the first handler has returned, so it can only land in the relock. With
main'sCond.cppthe test aborts on the acknowledgement timeout (0xc0000409).guest_raise_leaves_host_codeandguest_mutex_raise_exceptionpass with--repeat until-fail:3. The 48 related raise, mutex, cond, once and libc++ condition tests that are built pass with--repeat until-fail:3.Cond.cppandOnce.cppwere syntax-checked with g++ 11 on Linux (WSL Ubuntu).debt_fold.py --checkpasses. Not tested in a title.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)pthread_onceinitializer)🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Documentation